package com.disruptor.disruptorStudy.demo02;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import com.disruptor.disruptorStudy.demo01.Trade;
import com.lmax.disruptor.dsl.Disruptor;

//生产者
public class TradePublisher implements Runnable {
    private AtomicInteger at = new AtomicInteger(0);//生成递增id
    private Disruptor<Trade> disruptor;
    private CountDownLatch latch;  //线程监听
    private static int LOOP = 10;//模拟交易次数

    public TradePublisher(CountDownLatch latch, Disruptor<Trade> disruptor) {
        this.disruptor = disruptor;
        this.latch = latch;
    }

    @Override
    public void run() {
        //生产数据
        for (int i = 0; i < LOOP; i++) {
            disruptor.publishEvent( //发布
                    (event, sequence) -> {   //EventTranslator接口实现类(不带参数的)
                        //设置id
                        event.setId(String.valueOf(at.addAndGet(1)));
                    });
        }
        latch.countDown();//通知主线程继续执行
    }
}
